package ar.edu.caece.compiler.grammar.factories;

import ar.edu.caece.compiler.grammar.AbstractTokenVisitorFactory;
import ar.edu.caece.compiler.grammar.GrammarSymbol;
import ar.edu.caece.compiler.grammar.TokenVisitorInterface;
import ar.edu.caece.compiler.grammar.nonterminals.DECLAGLOBAL;
import ar.edu.caece.compiler.grammar.nonterminals.LISTADECLAGLOBAL;
import ar.edu.caece.compiler.grammar.nonterminals.LISTADECLAGLOBAL1;
import ar.edu.caece.compiler.grammar.terminals.GrammarTerminal;

public class LISTADECLAGLOBAL1Factory extends AbstractTokenVisitorFactory {

	private static LISTADECLAGLOBAL1Factory INSTANCE;
	
	public static LISTADECLAGLOBAL1Factory getInstance() throws Exception {
		if(INSTANCE==null) INSTANCE = new LISTADECLAGLOBAL1Factory();
		return INSTANCE;
	}
	
	private DECLAGLOBALFactory declaglobalFactory;
	private LISTADECLAGLOBALFactory listadeclaglobalesFactory;
	private TokenVisitorInterface parseDECLAGLOBAL;
	private TokenVisitorInterface parseLISTADECLAGLOBAL;
	
	private Initializer initializer = new Initializer() {
		
		@Override
		public void init() throws Exception {
			initializer = NoOperationInitializer.getInstance();
			declaglobalFactory = DECLAGLOBALFactory.getInstance();
			listadeclaglobalesFactory = LISTADECLAGLOBALFactory.getInstance();
			parseLISTADECLAGLOBAL = listadeclaglobalesFactory.makeVisitor();
			parseDECLAGLOBAL = declaglobalFactory.makeVisitor();
		}
	};
	
	private LISTADECLAGLOBAL1Factory() throws Exception {

	}
	
	@Override
	public TokenVisitorInterface makeChainedVisitor(
			final TokenVisitorInterface siguiente) throws Exception {
		initializer.init();

		return new TokenVisitorInterface() {

			@Override
			public GrammarSymbol defaultCase(GrammarTerminal llamador)
					throws Exception {
				GrammarSymbol result = llamador.execute(declaglobalFactory.makeChainedVisitor(siguiente));
				if(result instanceof DECLAGLOBAL) {
					return new LISTADECLAGLOBAL1 ((DECLAGLOBAL) result, (LISTADECLAGLOBAL) nextToken().execute(parseLISTADECLAGLOBAL)); 
				} 
				else {
					return result;
				}
			}
			
		};
	
	}
	@Override
	public TokenVisitorInterface makeVisitor() throws Exception {
		initializer.init();

		return new TokenVisitorInterface() {

			@Override
			public GrammarSymbol defaultCase(GrammarTerminal llamador) throws Exception {
				return new LISTADECLAGLOBAL1 ((DECLAGLOBAL) llamador.execute(parseDECLAGLOBAL), (LISTADECLAGLOBAL) nextToken().execute(parseLISTADECLAGLOBAL));
			}
		};
	}
	
	
}
